<?php

namespace app\admin\controller\collage;

use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;

/**
 * @ControllerAnnotation(title="拼团管理")
 */
class Collage extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\CollageCollage();

        $this->assign('getStatusList', $this->model->getStatusList());
        $this->assign('getNumberList', $this->model->getNumberList());
        $this->assign('getBargainTypeList', $this->model->getBargainTypeList());
        $this->assign('getBargainAddressList', $this->model->getBargainAddressList());
        $this->assign('getPayTypeList', $this->model->getPayTypeList());
        $this->field = "*,start_time as get_start_time,end_time as get_end_time,pay_type as get_pay_type";
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            if(empty($post['bargain_time'])){
                return Show::json_show(Status::status_error,"请选择砍价时间");
            }
            list($start,$end) = explode('~',$post['bargain_time']);
            $post['start_time'] = strtotime($start);
            $post['end_time'] = strtotime($end);
            $rule = [
                "goods_id"=>"require|integer",
                "goods_cate"=>"require|integer",
                "logo"=>"require",
                "stock"=>"require|integer",
                "end_price"=>"require",
                "start_time"=>"require",
                "end_time"=>"require",
                "number"=>"require",
                "type"=>"require|integer",
                "rule"=>"require"
            ];
            $message = [
                "goods_id.require"=>"请选择商品",
                "goods_cate.require"=>"请选择商品规格",
                "logo.require"=>"请上传封面图",
                "stock.require"=>"请输入库存",
                "end_price.require"=>"请输入最低价格",
                "start_time.require"=>"请选择砍价时间",
                "end_time.require"=>"请选择砍价时间",
                "number.require"=>"请输入砍价人数",
                "type.require"=>"请选择砍价方式",
                "rule.require"=>"请输入砍价规则"
            ];
            $this->validate($post, $rule,$message);


            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $row['rule'] = htmlspecialchars_decode($row['rule']);
        $row['bargain_time'] = date('Y-m-d H:i:s',$row['start_time'])." ~ ".date('Y-m-d H:i:s',$row['end_time']);
        $this->assign('row', $row);
        return $this->fetch();
    }


}